package com.cabrida.livestar.star;

import org.apache.http.Header;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.model.FansStarRecordData;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 粉丝端明星主页-明星档案
 */
public class StarRecordActivity extends BaseActivity {
	public static final String TAG = StarRecordActivity.class.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private TextView tv_cn_name;
	private TextView tv_fans_name;
	private TextView tv_constellation;
	private TextView tv_place;
	private TextView tv_works;
	private TextView tv_ach;
	private String suid;
	private FansStarRecordData recordData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}
	
	/** 填数据 */
	private void putData() {
		tv_title.setText(recordData.getS_name().toString());
		tv_cn_name.setText(recordData.getS_name().toString());
		tv_place.setText(recordData.getU_country().toString());
		tv_works.setText(recordData.getS_works().toString());
		tv_ach.setText(recordData.getS_record().toString());
	}
	
	/** 获取明星档案数据 */
	private void getRecordData() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("suid", suid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_FANS_STAR_RECORD_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getRecordData", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							recordData = new FansStarRecordData();
							recordData.setS_name(JsonUtils.getString(data,"s_name", ""));
							recordData.setU_country(JsonUtils.getString(data,"u_country", ""));
							recordData.setS_works(JsonUtils.getString(data,"s_works", ""));
							recordData.setS_record(JsonUtils.getString(data,"s_record", ""));
							putData();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_record);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_cn_name = (TextView) findViewById(R.id.tv_cn_name);
		tv_fans_name = (TextView) findViewById(R.id.tv_fans_name);
		tv_constellation = (TextView) findViewById(R.id.tv_constellation);
		tv_place = (TextView) findViewById(R.id.tv_place);
		tv_works = (TextView) findViewById(R.id.tv_works);
		tv_ach = (TextView) findViewById(R.id.tv_ach);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		suid = getIntent().getStringExtra("suid");
		getRecordData();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;

		default:
			break;
		}
	}
}
